<!DOCTYPE html>
<html>
<head>
    <title>游览全部路线</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 10px;
            background-color: #f5f6fa;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            text-align: center;
            padding: 0 10px;
            box-sizing: border-box;
        }

        h1 {
            color: #2c3e50;
            margin: 15px 0;
            font-size: 20px;
        }

        .route-info {
            background: white;
            padding: 12px 15px;
            margin: 10px auto;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 800px;
            text-align: left;
            box-sizing: border-box;
            line-height: 1.3;
            position: relative;
            padding-bottom: 50px;
        }

        .route-info p {
            margin: 5px 0;
        }

        .route-info strong {
            margin-right: 8px;
        }

        .route-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }

        .detail-item {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 6px;
            border-left: 4px solid #3498db;
            font-size: 18px;
        }

        .crowd-level {
            display: inline-block;
            padding: 5px 12px;
            border-radius: 12px;
            font-size: 18px;
            margin-left: 8px;
            font-weight: bold;
        }

        .map-container {
            margin: 15px auto;
            width: 100%;
            max-width: 800px;
            background: white;
            padding: 10px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            box-sizing: border-box;
        }

        .map-image {
            width: 100%;
            height: auto;
            max-width: 100%;
        }

        .button-grid {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin: 20px auto;
            width: 100%;
            max-width: 800px;
        }

        .button-row {
            display: flex;
            gap: 15px;
            justify-content: center;
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
        }

        .attraction-button {
            background-color: #c5e4f3;  /* 更深的背景色 */
            color: #2c3e50;
            border: none;
            padding: 16px 10px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 32px;
            font-weight: bold;
            transition: all 0.3s ease;
            width: calc(50% - 0px);
            text-align: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .attraction-button:hover {
            background-color: #a8d5ea;  /* 更深的悬停颜色 */
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .red-button {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 24px;  /* 增大字体 */
            font-weight: bold;  /* 加粗字体 */
            text-decoration: none;
            transition: all 0.3s ease;
            position: absolute;
            bottom: 15px;
            right: 20px;
        }

        .red-button:hover {
            background-color: #c0392b;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        /* 添加地图上人流量显示的样式 */
        .crowd-info {
            position: absolute;
            font-size: 26px;          /* 大字体 */
            font-weight: bold;
            padding: 8px 12px;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 6px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .crowd-number {
            font-size: 30px;          /* 数字更大 */
            color: #2c3e50;
        }

        .crowd-text {
            font-size: 24px;          /* 文字稍小一点 */
            color: #34495e;
        }

        /* 移动设备适配 */
        @media (max-width: 768px) {
            .container {
                padding: 5px;
            }

            h1 {
                font-size: 18px;
                margin: 10px 0;
            }

            .button-grid {
                gap: 10px;
            }

            .button-row {
                gap: 10px;
            }

            .attraction-button {
                font-size: 28px;
                padding: 12px 8px;
            }

            .route-info {
                padding: 14px;
                font-size: 18px;
            }

            .route-details {
                grid-template-columns: repeat(2, 1fr);
            }

            .detail-item {
                padding: 12px;
                font-size: 16px;
            }

            .crowd-level {
                font-size: 16px;
                padding: 4px 10px;
            }

            .map-container {
                padding: 8px;
                margin: 10px auto;
            }

            .red-button {
                font-size: 20px;
                padding: 10px 25px;
            }

            .crowd-info {
                font-size: 22px;
                padding: 6px 10px;
            }

            .crowd-number {
                font-size: 26px;
            }

            .crowd-text {
                font-size: 20px;
            }
        }

        /* 超小屏幕适配 */
        @media (max-width: 320px) {
            .button-grid {
                grid-template-columns: 1fr;
            }

            .attraction-button {
                font-size: 16px;
                padding: 12px 6px;
            }

            .route-info {
                padding: 12px;
                font-size: 16px;
            }

            .route-details {
                grid-template-columns: 1fr;
            }

            .detail-item {
                padding: 10px;
                font-size: 14px;
            }

            .crowd-level {
                font-size: 14px;
                padding: 3px 8px;
            }

            .red-button {
                font-size: 18px;
                padding: 8px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>动物园导览图</h1>
        
        <div class="route-info" id="result">
            <div>
                <p><strong>推荐路线：</strong><span id="routePath"></span></p>
                <p><strong>总距离：</strong><span id="totalDistance"></span></p>
            </div>
            <a href="/" class="red-button">返回主菜单</a>
        </div>
        
        <div class="map-container">
            <img id="map" src="data:image/png;base64,{{initial_map}}" class="map-image">
        </div>

        <!-- 修改按钮网格布局 -->
        <div class="button-grid">
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('金丝猴馆')">金丝猴馆</button>
                <button class="attraction-button" onclick="selectAttraction('鹿苑')">鹿苑</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('两栖爬行馆')">两栖爬行馆</button>
                <button class="attraction-button" onclick="selectAttraction('科普馆')">科普馆</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('火烈鸟馆')">火烈鸟馆</button>
                <button class="attraction-button" onclick="selectAttraction('企鹅乐园')">企鹅乐园</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('非洲动物区')">非洲动物区</button>
                <button class="attraction-button" onclick="selectAttraction('儿童动物园')">儿童动物园</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('猿猴馆')">猿猴馆</button>
                <button class="attraction-button" onclick="selectAttraction('长颈鹿馆')">长颈鹿馆</button>
            </div>
        </div>
    </div>

    <script>
        function selectAttraction(attraction) {
            // 显示加载提示
            document.getElementById('result').innerHTML = '正在计算路线...';
            
            fetch('/calculate_route', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    'type': 'full',
                    'start': attraction,
                    'route_type': 'full'  // 添加额外的标识
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    const resultDiv = document.getElementById('result');
                    resultDiv.innerHTML = `
                        <div>
                            <p><strong>推荐路线：</strong>${data.path.join(' → ')}</p>
                            <p><strong>总距离：</strong>${data.distance} 米</p>
                        </div>
                        <a href="/" class="red-button">返回主菜单</a>
                    `;
                    
                    const mapImg = document.getElementById('map');
                    mapImg.src = `data:image/png;base64,${data.map}`;
                } else {
                    document.getElementById('result').innerHTML = `
                        <div class="error">
                            计算路线失败：${data.message}
                        </div>
                        <a href="/" class="red-button">返回主菜单</a>
                    `;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('result').innerHTML = `
                    <div class="error">
                        请求失败，请稍后重试
                    </div>
                    <a href="/" class="red-button">返回主菜单</a>
                `;
            });
        }

        // 页面加载时显示初始地图
        window.onload = function() {
            fetch('/calculate_route', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    'type': 'full'  // 初始化时也指定类型
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    const mapImg = document.getElementById('map');
                    if (mapImg) {
                        mapImg.src = `data:image/png;base64,${data.map}`;
                    }
                }
            });
        }

        // 根据人流量获取颜色
        function getCrowdLevelColor(crowd) {
            if (crowd < 30) return '#2ecc71';  // 绿色 - 通畅
            if (crowd < 60) return '#f1c40f';  // 黄色 - 一般
            if (crowd < 80) return '#e67e22';  // 橙色 - 较拥挤
            return '#e74c3c';                  // 红色 - 拥挤
        }

        // 根据人流量获取文字描述
        function getCrowdLevelText(crowd) {
            if (crowd < 30) return '通畅';
            if (crowd < 60) return '一般';
            if (crowd < 80) return '较拥挤';
            return '拥挤';
        }

        // 生成路线建议
        function getRouteAdvice(path, crowdData) {
            let highCrowdPoints = path.filter(point => crowdData[point] >= 70);
            let advice = '';
            
            if (highCrowdPoints.length > 0) {
                advice += `建议提前游览${highCrowdPoints.join('、')}等人流量较大的景点。`;
            }
            
            let lowCrowdPoints = path.filter(point => crowdData[point] < 30);
            if (lowCrowdPoints.length > 0) {
                advice += `${lowCrowdPoints.join('、')}等景点人流量较少，可以放心游玩。`;
            }
            
            if (!advice) {
                advice = '当前路线整体人流量适中，可以按推荐顺序游览。';
            }
            
            return advice;
        }

        // 页面加载时更新人流量数据
        fetch('/update_crowd_data')
            .then(response => response.json())
            .then(data => {
                if (data.status !== 'success') {
                    console.error('更新人流量数据失败：', data.message);
                }
            });
    </script>
</body>
</html>